<template>
	<view class="container">
		<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList"
			orderby="rata desc">
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="data">

				<!-- {{item}} -->
				<uni-card v-for="(item, index) in data" :key="index" @click="handleItemClick(item._id)"
					:title="item.name" :sub-title="item.address" :extra="item.type"
					thumbnail="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png">
					<view style="display:flex;justify-content: space-between;margin-bottom:10px">
						<view style="display:flex;">
							评分：<uni-rate :readonly="true" v-model="item.rata" @change="onChange" />
						</view>
						<view>
							<text v-if="!item.rata"
								:style="{color:item.rata?'':'orange'}">{{item.rata?'':'评分人数不足'}}</text>
							<template v-else>
								<uni-tag v-if="item.rata==5" size="small" :inverted="true"
									:type="item.rata==5?'success':''" text="神仙企业"></uni-tag>
								<uni-tag v-else size="small" :inverted="true" :type="item.rata>2?'success':'error'"
									:text="item.rata>2?'推荐':'避雷'"></uni-tag>

							</template>

						</view>

					</view>

					<text style="color:#bbb">{{item.content}}</text>
				</uni-card>
				<!-- <text> -->
				<!-- 此处默认显示为_id，请根据需要自行修改为其他字段 -->
				<!-- 如果使用了联表查询，请参考生成的 admin 项目中 list.vue 页面 -->
				<!-- {{item}} -->
				<!-- </text> -->


			</view>
			<uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
		</unicloud-db>
		<uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" />
	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				ratevalue: 5,
				typeList: [{
						"text": "公司环境",
						"value": 1
					},
					{
						"text": "人际关系",
						"value": 2
					},
					{
						"text": "加班情况",
						"value": 3
					},
					{
						"text": "工作难度",
						"value": 3
					}
				],
				collectionList: [db.collection('gongsi').field('name,city_id,address,type,content,rata', 'createTime')
					.getTemp(), db
					.collection('city').field('name as text, _id').getTemp()
				],
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				}
			}
		},
		onPullDownRefresh() {
			this.$refs.udb.loadData({
				clear: true
			}, () => {
				uni.stopPullDownRefresh()
			})
		},
		onShow() {
			if (this.$refs.udb) {
				this.$refs.udb.loadData({
					clear: true
				})
			}
		},
		onReachBottom() {
			this.$refs.udb.loadMore()
		},
		methods: {
			t2f(timestamp) {
				if (!timestamp) {
					return ''
				}
				var date = new Date(timestamp); // 转换为Date对象
				var year = date.getFullYear(); // 获取年份
				var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份并补零
				var day = date.getDate().toString().padStart(2, '0'); // 获取日期并补零
				var hours = date.getHours().toString().padStart(2, '0'); // 获取小时并补零
				var minutes = date.getMinutes().toString().padStart(2, '0'); // 获取分钟并补零
				var seconds = date.getSeconds().toString().padStart(2, '0'); // 获取秒钟并补零

				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 拼接字符串返回
			},
			handleItemClick(id) {
				uni.navigateTo({
					url: './detail?id=' + id
				})
			},
			fabClick() {
				// 打开新增页面
				uni.navigateTo({
					url: './add',
					events: {
						// 监听新增数据成功后, 刷新当前页面数据
						refreshData: () => {
							this.$refs.udb.loadData({
								clear: true
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>